#include "ParseBib.h"
#include <windows.h>

vector<string> bibFile;

bool IsRoot(string Path)
{
    string Root;
    Root = Path.at(0)+":\\";
    if (Root == Path)
       return true;
    else
       return false;
}

void FindAllBibFile(string Path)
{
    string szFind;
    szFind = Path;
    if (!IsRoot(szFind))
       szFind += "\\";
    szFind += "*.bib";
    WIN32_FIND_DATA FindFileData;
    HANDLE hFind=FindFirstFile(szFind.c_str(),& FindFileData);
    if(hFind==INVALID_HANDLE_VALUE)
       return ;
    do
    {
       if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) //find .bib file
       {
           cout<<FindFileData.cFileName<<endl;
           bibFile.push_back(FindFileData.cFileName);
       }
    } while (FindNextFile(hFind,& FindFileData));
    FindClose(hFind);
}

int main(int argc, char* args[]) {
    if (argc == 1 ) {
       char cf[100];
       GetCurrentDirectory(100, cf);
       printf("%s\n", cf);
       FindAllBibFile(cf);
       int size = bibFile.size();
       for (int i = 0; i < size; i++) {
           ParseBib p(bibFile[i], string("tiyed_") + bibFile[i]);
           p.saveTidyedBibFile();
       }
    } else 
    if (argc == 3 ) {
       ParseBib p(args[1], args[2]);
       p.saveTidyedBibFile();
    }
    return 0;
}
